package org.mms.controller;

import org.mms.pojo.dto.VehicleDto;
import org.mms.pojo.entity.Vehicle;
import org.mms.pojo.request.VehicleRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @PostMapping
    public ResponseMessage<VehicleDto> create(@RequestBody VehicleRequest request) {
        return ResponseMessage.success(vehicleService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<VehicleDto> update(@RequestBody VehicleRequest request) {
        return ResponseMessage.success(vehicleService.update(request)
                .convert());
    }

    @GetMapping
    public ResponseMessage<List<VehicleDto>> list() {
        return ResponseMessage.success(vehicleService.list()
                .stream()
                .map(Vehicle::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<VehicleDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(vehicleService.getById(id)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<VehicleDto> delete(@PathVariable Long id) {
        vehicleService.delete(id);
        return ResponseMessage.success(null);
    }
}